package com.suishoupai.xmb.utils;

import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by caojian on 2017/4/7.
 */
public class ValidateUtil {
    /**
     * 最小值(若为String，取length)
     *
     * @param o
     * @param min
     * @return
     */
    public static void min(Object o, int min) {
        notNull(o);
        if (o instanceof String) {
            String s = (String) o;
            if (s.length() < min) {
                throwException();
            }
        } else if (o instanceof Integer) {
            int i = (Integer) o;
            if (i < min) {
                throwException();
            }
        } else if (o instanceof Long) {
            long l = (Long) o;
            if (l < min) {
                throwException();
            }
        } else if (o instanceof List) {
            List list = (List) o;
            if (list.size() < min) {
                throwException();
            }
        } else {
            //其他类型不认识
            throwException();
        }
    }

    /**
     * 最大值(若为String，取length)
     *
     * @param o
     * @param max
     * @return
     */
    public static void max(Object o, int max) {
        notNull(o);
        if (o instanceof String) {
            String s = (String) o;
            if (s.length() > max) {
                throwException();
            }
        } else if (o instanceof Integer) {
            int i = (Integer) o;
            if (i > max) {
                throwException();
            }
        } else if (o instanceof Long) {
            long l = (Long) o;
            if (l > max) {
                throwException();
            }
        } else if (o instanceof List) {
            List list = (List) o;
            if (list.size() > max) {
                throwException();
            }
        } else {
            //其他类型不认识
            throwException();
        }
    }

    /**
     * 必须为email
     *
     * @return
     */
    public static void isEmail(Object o) {
        notNull(o);
        if (!(o instanceof String)) {
            throwException();
        }
        String email = (String) o;
        if (!email.matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$")) {
            throwException();
        }
    }

    /**
     * o必须在objects集合中
     *
     * @param o
     * @param objects
     */
    public static void in(Object o, Object... objects) {
        boolean isString = o != null && (o instanceof String);
        for (Object temp : objects) {
            if (temp == o || (isString && o.toString().equals(temp))) {
                return;
            }
        }
        throwException();
    }

    /**
     * 不能为空
     *
     * @param o
     * @return
     */
    public static void notNull(Object o) {
        if (StringUtils.isEmpty(o)) {
            throwException();
        }
    }

    /**
     * 必须为空
     *
     * @param o
     * @return
     */
    public static void isNull(Object o) {
        if (!StringUtils.isEmpty(o)) {
            throwException();
        }
    }

    private static void throwException() {
        throw new RuntimeException("参数不合法");
    }
}
